
#include "matching.h"
#include "../../engine.h"

#include "../../User.h"

#include "../../NLogic/Static.h"
#include "../../NLogic/Function.h"

namespace ProjectZ { namespace NCommand { namespace NMatching {

void
CS_DUNGEON_MOB_CREATE::OnExecute(User * pUser, ::Network::Packet * pPacket)
{
	/*
	 * 방장 : 몬스터 세팅요청 C->S
	CS_DUNGEON_MOB_CREATE	0x0805		2	unsigned short	좌표X
				2	unsigned short	좌표Y
				2	unsigned short	몬스터 TID
				2	unsigned short	몬스터 LV
				1	char	레전드 여부
				2	unsigned short	"방향 벡터(보고있는 방향
	0 : RIGHT, 90 : DOWN..)"

	모두 : 몬스터 세팅 정보 ( Server Notification Packet ) - 몬스터 생성시
	SN_DUNGEON_MOB_CREATE	0x0807		2	unsigned short	좌표X
				2	unsigned short	좌표Y
				2	unsigned short	몬스터 TID
				2	unsigned short	몬스터 LV
				1	char	레전드 출현 여부
				2	unsigned short	"방향 벡터(보고있는 방향
	0 : RIGHT, 90 : DOWN..)"
				4	unsigned int	GID : 게임 유니크 Key
				4	unsigned int	hp_max
	 *
	 */
	NLogic::Space * pSpace = pUser->GetSpace();
	if (NULL == pSpace)
	{
		DEBUGLOG("WARNING: CS_DUNGEON_MOB_CREATE GID %d, pSpace is NULL");
		return;
	}

	if (false == pSpace->IsDungeon())
	{
		DEBUGLOG("CS_DUNGEON_MOB_CREATE space is not single or party dungeion");
		return;
	}

	if (true == pSpace->IsPartyDungeon())
	{
		NLogic::Party * pParty = pUser->GetParty();
		if (NULL == pParty)
		{
			DEBUGLOG("CS_DUNGEON_MOB_CREATE NULL == pParty");
			return;
		}

//		if (pParty->GetMaster() != pUser)
//		{
//			DEBUGLOG("CS_DUNGEON_MOB_CREATE pParty->GetMaster() != pUser");
//			return;
//		}
	}

	PREPARE_PACKET;

//	int x = 0;
//	int y = 0;
//	int tid = 0;
//	int level = 0;
//	int legend = 0;
//	int degree = 0;
//	int gid = 0;

	int16_t x = 0;
	int16_t y = 0;
	int16_t tid = 0;
	int16_t level = 0;
	int8_t legend = 0;
	int16_t degree = 0;
	int gid = 0;

	* pPacket >> x >> y >> tid >> level >> legend >> degree;

	DEBUGLOG("x : %d y : %d tid : %d level : %d legend : %d degree : %d", x, y, tid, level, legend, degree);

	//pPacket->ParseMsg("%2d%2d%2d%2d%c%2d", &x, &y, &tid, &level, &legend, &degree);

	gid = pSpace->GetMonsterGID() + 1;

	int dungeon_mode = pSpace->GetData()._difficulty;
	int player_count = pSpace->GetData()._currentUser;
	int table_id = pSpace->GetData()._table_id;
	bool bBoss = (pSpace->GetData()._boss_tid == tid) ? true : false;

	int monster_hp = NLogic::NFunction::GetMonsterHP(pUser, level, dungeon_mode, table_id, player_count, legend, bBoss);

	::Network::Packet * pNoti = ::Network::GETPACKETPOOL()->SEND_NEW();
	//pNoti->MsgCat("%2d%2d%2d%2d%c%2d%d%d", x, y, tid, level, legend, degree, gid, monster_hp);
	* pNoti << x << y << tid << level << legend << degree << gid << monster_hp;
	pNoti->MakePacket(CMD_SN_DUNGEON_MOB_CREATE);
	pSpace->SendAll(pNoti);

	//
	//::ProjectZ::NGame::CMonster *monster = new ::ProjectZ::NGame::CMonster;
	NLogic::Monster::Data clsData;
	clsData.gid = gid;
	clsData.hp = monster_hp;
	clsData.max_hp = monster_hp;
	clsData.map_index = -1;
	clsData.x = (x * 32);
	clsData.y = (y * 32);
	clsData.tid = tid;
	clsData.level = level;
	clsData.islegend = legend;
	clsData.degree = degree;
	clsData.isslave = 0;

	NLogic::Monster * pMonster = NLogic::Static::Instance().NEW_MONSTER();
	pMonster->Open(clsData, NLogic::Monster::MonsterType::MONSTER);

	pSpace->AddMonster(pMonster);
}

} /*NMatching*/ } /*NCommand*/ } /*ProjectZ*/
